<template>
  <div>
    <el-dialog :visible="vischaracter" :title="ruleForm.id?'编辑角色':'添加角色'" width="50%" @close="celbtn">
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-form-item label="角色名称" prop="title">
          <el-input v-model="ruleForm.title" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="ruleForm.description" />
        </el-form-item>
      </el-form>
      <el-row type="flex" justify="center">
        <el-button type="primary" @click="addrolebtn">确认</el-button>
        <el-button @click="celbtn"> 取消</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
import { postrole, getroledetail, putrole } from '@/api/character'

export default {
  props: {
    vischaracter: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      ruleForm: {
        title: '',
        description: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 确定按钮
    addrolebtn() {
      this.$refs.ruleForm.validate(async(valid) => {
        if (valid) {
          if (this.ruleForm.id) {
            // 编辑角色
            await putrole(this.ruleForm)
            this.$message({
              message: '编辑成功',
              type: 'success'
            })
          } else {
            // 添加角色
            await postrole(this.ruleForm)
            this.$message({
              message: '添加成功',
              type: 'success'
            })
          }

          //   关闭弹窗
          this.celbtn()
          //   渲染页面
          this.$emit('getlist')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 获取编辑用户信息
    async getputrole(id) {
      const res = await getroledetail(id)
      this.ruleForm = res.data
    },
    // 取消按钮
    celbtn() {
      this.ruleForm = {
        title: '',
        description: ''
      }
      this.$refs.ruleForm.resetFields()
      this.$emit('update:vischaracter', false)
    }
  }
}
</script>
<style>

</style>
